<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 顶部搜索和添加区域 -->
      <el-row :gutter="30">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear='getGoodsList'
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addGoods"
          >添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品表格区域 -->
      <el-table
        :data='goods_list'
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
        ></el-table-column>
        <el-table-column
          label="商品价格（元）"
          prop="goods_price"
          width="70"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          prop="add_time"
          width="120"
        >
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="160"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="delDialogShow(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 5, 10, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 删除确认弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="delDialogVisible"
      width="50%"
    >
      <span>确认删除吗</span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="deleteGoods"
        >删除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 请求头需要的数据
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 获取商品分类数据保存在本地用于渲染页面
      goods_list: [],
      total: 0,
      delDialogVisible: false, // 控制删除确认弹窗
      goodsID: 0 // 保存商品的ID
    }
  },
  created () {
    this.getGoodsList() // 获取商品分类数据
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取商品分类失败')
      this.goods_list = res.data.goods
      this.total = res.data.total
      // console.log(this.goods_list)
    },
    // 分页功能
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList() // 赋值以后要重新获取数据进行渲染
    },
    handleCurrentChange (newcurrent) {
      this.queryInfo.pagenum = newcurrent
      this.getGoodsList()
    },
    // 点击删除按钮删除商品
    async deleteGoods () {
      const { data: res } = await this.$http.delete(`goods/${this.goodsID}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.delDialogVisible = false
      this.goodsID = 0
      this.getGoodsList() // 获取商品分类数据
    },
    // 点击按钮淡出删除确认弹窗
    delDialogShow (id) {
      this.goodsID = id
      this.delDialogVisible = true
    },
    // 跳转添加商品页面
    addGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}
</style>
